package org.linlinjava.litemall.db.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.linlinjava.litemall.db.dao.RegionMapper;
import org.linlinjava.litemall.db.models.Region;
import org.linlinjava.litemall.db.service.RegionService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class RegionServiceImpl extends ServiceImpl<RegionMapper, Region>
        implements RegionService {

    @Override
    public List<Region> getAll(){
        return baseMapper.selectList(new LambdaQueryWrapper<Region>().ne(Region::getType, 4));
    }

    @Override
    public List<Region> queryByPid(Long parentId) {
        return baseMapper.selectList(new LambdaQueryWrapper<Region>().ne(Region::getPid, parentId));
    }

    @Override
    public IPage<Region> querySelective(String name, Integer code, Integer pageNo, Integer pageSize) {
        Page<Region> page =new Page<>(pageNo, pageSize);
        QueryWrapper<Region> query = new QueryWrapper<>();
        query.like(!StringUtils.isEmpty(name), "name", name);
        query.eq(!StringUtils.isEmpty(code), "code", code);
        return baseMapper.selectPage(page, query);
    }

}
